package com.bsoft.gol.editor.service.impl;


import com.bsoft.gol.editor.dao.EditorDisplayLogicDAO;
import com.bsoft.gol.editor.dao.EditorQuestionnaireTitleDAO;
import com.bsoft.gol.editor.editor.EditorDisplayLogic;
import com.bsoft.gol.editor.editor.EditorQuestionnaireTitle;
import com.bsoft.gol.editor.editor.request.EditorSkipLogicListReq;
import hcn.util.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.bsoft.gol.editor.service.EditorDisplayLogicApi;
import com.bsoft.gol.editor.utils.CommonUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;


/**
 * 显示逻辑表
 */
@Service("editorDisplayLogicApi")
@Transactional
public class EditorDisplayLogicApiImpl implements EditorDisplayLogicApi {

	@Autowired
	EditorDisplayLogicDAO editorDisplayLogicDAO;

	@Autowired
	EditorQuestionnaireTitleDAO editorQuestionnaireTitleDAO;

	@Override
	public void addDisplayLogic(EditorDisplayLogic req) {

		editorDisplayLogicDAO.save(req);
	}

	@Override
	public Map<String, Object> saveBatch(EditorDisplayLogic req) {
		Map<String, Object> resMap = new HashMap<String, Object>();
		for (EditorSkipLogicListReq skipLogicListReq : req.getOptions()) {
			EditorDisplayLogic editorDisplayLogic = new EditorDisplayLogic();
			if (CommonUtils.isNull(skipLogicListReq.getOptionId())
					|| CommonUtils.isNull(skipLogicListReq.getTitleDisplayId())
					|| CommonUtils.isNull(skipLogicListReq.getConditionType())) {
				resMap.put("code", ResultCode.PARAMETERS_EMPTY);
				resMap.put("message", "传入参数不能为空");
				return resMap;
			}
			editorDisplayLogic.setDisplayId(UUID.randomUUID().toString().replaceAll("-", ""));
			editorDisplayLogic.setTitleId(req.getTitleId());
			editorDisplayLogic.setOptionId(skipLogicListReq.getOptionId());
			editorDisplayLogic.setTitleDisplayId(skipLogicListReq.getTitleDisplayId());
			if (skipLogicListReq.getSelectCondition() != null) {
				editorDisplayLogic.setSelectCondition(skipLogicListReq.getSelectCondition());
			}
			editorDisplayLogic.setConditionType(skipLogicListReq.getConditionType());
			editorDisplayLogicDAO.save(editorDisplayLogic);

			//将显示题目标识为有显示逻辑
			EditorQuestionnaireTitle questionnaireTitle = new EditorQuestionnaireTitle();
			questionnaireTitle.setTitleId(req.getTitleId());//被标识的显示题
			questionnaireTitle.setDisplayLogicFlag((byte) 1);
			editorQuestionnaireTitleDAO.updateEditorQuestionnaireTitleFlag(req.getTitleId(),(byte) 1);
		}

		//将该题目标识为不显示
		EditorQuestionnaireTitle editorQuestionnaireTitle = new EditorQuestionnaireTitle();
		editorQuestionnaireTitle.setTitleId(req.getTitleId());
		editorQuestionnaireTitle.setDisplayFlag((byte) 2);
		editorQuestionnaireTitleDAO.updateEditorQuestionnaireTitleFlag(req.getTitleId(),(byte) 2);


		resMap.put("code", ResultCode.SUCCESS);
		return resMap;
	}


	@Override
	public void deleteDisplayLogic(EditorDisplayLogic req) {
		editorDisplayLogicDAO.remove(req.getDisplayId());
	}

	@Override
	public List<EditorDisplayLogic> list(EditorDisplayLogic req) {

		return editorDisplayLogicDAO.queryEditorDisplayLogicListByTitleId(req.getTitleId());
	}
}